{include="header"}

{if condition="$fsc->factura"}
<script type="text/javascript">
   $(document).ready(function() {
      $("#b_eliminar, #b_eliminar2").click(function(event) {
         event.preventDefault();
         $("#modal_eliminar").modal('show');
      });
      {if condition="$fsc->factura->totalrecargo==0"}
      $(".recargo").hide();
      {/if}
      {if condition="$fsc->factura->totalirpf==0"}
      $(".irpf").hide();
      {/if}
   });
</script>

<form action="{$fsc->factura->url()}" method="post" class="form">
   <input type="hidden" name="idfactura" value="{$fsc->factura->idfactura}"/>
   <div class="container-fluid">
      <div class="row" style="margin-top: 10px;">
         <div class="col-md-8 col-sm-8 col-xs-12">
            <a class="btn btn-sm btn-default" href="{$fsc->url()}" title="recargar la página">
               <span class="glyphicon glyphicon-refresh"></span>
            </a>
            
            {if condition="$fsc->factura->idasiento"}
            <a class="btn btn-sm btn-default" href="{$fsc->factura->asiento_url()}">
               <span class="glyphicon glyphicon-eye-open"></span> &nbsp; Asiento contable
            </a>
            {else}
            <a class="btn btn-sm btn-default" href="{$fsc->url()}&gen_asiento=TRUE&petid={$fsc->random_string()}">
               <span class="glyphicon glyphicon-paperclip"></span> &nbsp; Generar asiento
            </a>
            {/if}
            
            <div class="btn-group">
               {if condition="$fsc->factura->pagada"}
               <button type="button" class="btn btn-sm btn-info dropdown-toggle" data-toggle="dropdown">
                  <span class="glyphicon glyphicon-ok"></span>
                  &nbsp; Pagada <span class="caret"></span>
               </button>
               {else}
               <button type="button" class="btn btn-sm btn-warning dropdown-toggle" data-toggle="dropdown">
                  <span class="glyphicon glyphicon-remove"></span>
                  &nbsp; Sin pagar <span class="caret"></span>
               </button>
               {/if}
               <ul class="dropdown-menu" role="menu">
                  {if condition="!$fsc->factura->pagada"}
                  <li><a href="{$fsc->url()}&pagada=TRUE"><span class="glyphicon glyphicon-ok"></span> &nbsp; Pagada</a></li>
                  {else}
                  <li><a href="{$fsc->url()}&pagada=FALSE"><span class="glyphicon glyphicon-remove"></span> &nbsp; Sin pagar</a></li>
                  {/if}
               </ul>
            </div>
            
            <div class="btn-group">
            {loop="$fsc->extensions"}
               {if condition="$value->type=='button'"}
               <a href="index.php?page={$value->from}&id={$fsc->factura->idfactura}" class="btn btn-sm btn-default">
                  {$value->text}
               </a>
               {/if}
            {/loop}
            </div>
         </div>
         <div class="col-md-4 col-sm-4 hidden-xs text-right">
            <div class="btn-group">
               <a id="b_eliminar" class="btn btn-sm btn-danger" href="#">
                  <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
               </a>
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
      <div class="row hidden-lg hidden-md hidden-sm">
         <div class="col-xs-12 text-right" style="margin-top: 10px;">
            <div class="btn-group">
               <a id="b_eliminar2" class="btn btn-sm btn-danger" href="#">
                  <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
               </a>
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
      <div class="row">
         <div class="col-lg-12">
            <h3 style="margin-bottom: 0px;">
               <a href="{$fsc->ppage->url()}">Facturas</a> /
               <a href="{$fsc->factura->proveedor_url()}">{$fsc->factura->nombre}</a> /
               {$fsc->factura->codigo}
            </h3>
            {if condition="$fsc->agente"}
            <p>Factura creada por <a href="{$fsc->agente->url()}">{$fsc->agente->get_fullname()}</a>.</p>
            {/if}
         </div>
      </div>
      
      <div class="row">
         <div class="col-lg-2 col-md-2 col-sm-2">
            <div class="form-group">
               Num. Proveedor:
               <input class="form-control" type="text" name="numproveedor" value="{$fsc->factura->numproveedor}"/>
            </div>
         </div>
         <div class="col-lg-2 col-md-2 col-sm-2">
            <div class="form-group">
               Serie:
               <div class="form-control">{$fsc->factura->codserie}</div>
            </div>
         </div>
         <div class="col-lg-2 col-md-2 col-sm-2">
            <div class="form-group">
               Ejercicio:
               <div class="form-control">{$fsc->factura->codejercicio}</div>
            </div>
         </div>
         <div class="col-lg-2 col-md-2 col-sm-2">
            <div class="form-group">
               Fecha:
               <input class="form-control datepicker" type="text" name="fecha" value="{$fsc->factura->fecha}"/>
            </div>
         </div>
      </div>
   </div>
   
   <div role="tabpanel">
      <ul class="nav nav-tabs" role="tablist">
         <li role="presentation" class="active">
            <a href="#lineas_f" aria-controls="lineas_f" role="tab" data-toggle="tab">Líneas</a>
         </li>
         <li role="presentation">
            <a href="#lineas_iva" aria-controls="lineas_iva" role="tab" data-toggle="tab">Impuestos</a>
         </li>
         {loop="$fsc->extensions"}
            {if condition="$value->type=='tab'"}
            <li role="presentation">
               <a href="#ext_{$value->name}" aria-controls="ext_{$value->name}" role="tab" data-toggle="tab">{$value->text}</a>
            </li>
            {/if}
         {/loop}
      </ul>
      <div class="tab-content">
         <div role="tabpanel" class="tab-pane active" id="lineas_f">
            <div class="table-responsive">
               <table class="table table-hover">
                  <thead>
                     <tr>
                        <th class="text-left text-capitalize">{#FS_ALBARAN#}</th>
                        <th class="text-left">Artículo</th>
                        <th class="text-right">Cantidad</th>
                        <th class="text-right">PVP</th>
                        <th class="text-right">Dto</th>
                        <th class="text-right">Neto</th>
                        <th class="text-right">IVA</th>
                        <th class="recargo text-right">RE</th>
                        <th class="irpf text-right">IRPF</th>
                        <th class="text-right">Total</th>
                     </tr>
                  </thead>
                  {loop="$fsc->factura->get_lineas()"}
                  <tr{if condition="$value->cantidad<1"} class="bg-warning"{/if}>
                     <td>
                        {if condition="$value->idalbaran"}
                        <a href="{$value->albaran_url()}">{$value->albaran_codigo()}</a>
                        {$value->albaran_numero()}
                        {else}
                        -
                        {/if}
                     </td>
                     <td>
                        <a href="{$value->articulo_url()}">{$value->referencia}</a>
                        {$value->descripcion}
                     </td>
                     <td class="text-right">{$value->cantidad}</td>
                     <td class="text-right">{$fsc->show_precio($value->pvpunitario, $fsc->factura->coddivisa)}</td>
                     <td class="text-right">{$fsc->show_numero($value->dtopor, 2)} %</td>
                     <td class="text-right">{$fsc->show_precio($value->pvptotal, $fsc->factura->coddivisa)}</td>
                     <td class="text-right">{$fsc->show_numero($value->iva, 2)} %</td>
                     <td class="recargo text-right">{$fsc->show_numero($value->recargo, 2)} %</td>
                     <td class="irpf text-right">{$fsc->show_numero($value->irpf, 2)} %</td>
                     <td class="text-right">{$fsc->show_precio($value->total_iva(), $fsc->factura->coddivisa)}</td>
                  </tr>
                  {/loop}
                  <tr>
                     <td colspan="5"></td>
                     <td class="text-right"><b>{$fsc->show_precio($fsc->factura->neto, $fsc->factura->coddivisa)}</b></td>
                     <td class="text-right"><b>{$fsc->show_precio($fsc->factura->totaliva, $fsc->factura->coddivisa)}</b></td>
                     <td class="recargo text-right"><b>{$fsc->show_precio($fsc->factura->totalrecargo, $fsc->factura->coddivisa)}</b></td>
                     <td class="irpf text-right"><b>-{$fsc->show_precio($fsc->factura->totalirpf, $fsc->factura->coddivisa)}</b></td>
                     <td class="text-right"><b>{$fsc->show_precio($fsc->factura->total, $fsc->factura->coddivisa)}</b></td>
                  </tr>
               </table>
            </div>
         </div>
         <div role="tabpanel" class="tab-pane" id="lineas_iva">
            <div class="table-responsive">
               <table class="table table-hover">
                  <thead>
                     <tr>
                        <th class="text-left">Impuesto</th>
                        <th class="text-right">Neto</th>
                        <th class="text-right">IVA</th>
                        <th class="text-right">Total IVA</th>
                        <th class="text-right">RE</th>
                        <th class="text-right">Total RE</th>
                        <th class="text-right">Total</th>
                     </tr>
                  </thead>
                  {loop="$fsc->factura->get_lineas_iva()"}
                  <tr>
                     <td>{$value->codimpuesto}</td>
                     <td class="text-right">{$fsc->show_precio($value->neto, $fsc->factura->coddivisa)}</td>
                     <td class="text-right">{$fsc->show_numero($value->iva, 2)} %</td>
                     <td class="text-right">{$fsc->show_precio($value->totaliva, $fsc->factura->coddivisa)}</td>
                     <td class="text-right">{$fsc->show_numero($value->recargo, 2)} %</td>
                     <td class="text-right">{$fsc->show_precio($value->totalrecargo, $fsc->factura->coddivisa)}</td>
                     <td class="text-right">{$fsc->show_precio($value->totallinea, $fsc->factura->coddivisa)}</td>
                  </tr>
                  {/loop}
               </table>
            </div>
         </div>
         {loop="$fsc->extensions"}
            {if condition="$value->type=='tab'"}
            <div role="tabpanel" class="tab-pane" id="ext_{$value->name}">
               <iframe src="index.php?page={$value->from}{$value->params}&id={$fsc->factura->idfactura}" width="100%" height="600" frameborder="0"></iframe>
            </div>
            {/if}
         {/loop}
      </div>
   </div>
   
   <div class="container-fluid">
      <div class="row">
         <div class="col-lg-12 col-md-12 col-sm-12">
            <div class="form-group">
               Observaciones:
               <textarea class="form-control" name="observaciones" rows="3">{$fsc->factura->observaciones}</textarea>
            </div>
         </div>
      </div>
   </div>
</form>

<div class="modal fade" id="modal_eliminar">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3 class="modal-title">¿Realmente desea eliminar esta factura?</h3>
         </div>
         {if condition="$fsc->factura->idasiento"}
         <div class="modal-body">
            Hay asociado un asiento contable que será eliminado junto con la factura.
         </div>
         {/if}
         <div class="modal-footer">
            <a class="btn btn-sm btn-danger" href="{$fsc->ppage->url()}&delete={$fsc->factura->idfactura}">
               <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
            </a>
         </div>
      </div>
   </div>
</div>
{else}
<div class="text-center">
   <img src="view/img/fuuu_face.png" alt="fuuuuu"/>
</div>
{/if}

{include="footer"}